<?php
namespace app\wadmin\controller;
// +----------------------------------------------------------------------
// | bbcThink
// +----------------------------------------------------------------------
// | Copyright (c) 2025 http://bbcweb.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: bbcweb <22745529@qq.com>
// +----------------------------------------------------------------------
use app\BaseController;
use think\facade\Db;
use think\facade\View;

//客户单位管理
class Shop extends BaseController
{
    public $table = 'shop';
    public function initialize()
    {
        parent::initialize();
    }
    public function index()
    {
        $p=input();
        $where=array();
        if(!empty($p['keyword']))
        {
            $where[]=['name','like','%'.$p['keyword'].'%'];
        }
        
        $data_list =\app\service\ShopService::paginate(['where'=>$where,'query'=>$p]);
        $list=array();
        foreach($data_list['list'] as $v)
        {
            $v['admin_name']='';
            $list[]=$v;
        }
        $page = $data_list['pager'];
        View::assign('data_list',$list);
        View::assign('page',$page);
        View::assign('user',$this->admin);
        return View::fetch();
    }
   
    public function modify()
    {
        $p=input();
        if($this->request->isPost())
        {
            unset($p['file']);
            $p['county_id']=empty($p['county_id'])?'':$p['county_id'];
            $p['country_id']=cache('lang_id');
            if(empty($p['id']))
            {
                $p['add_time']=time();
                $res = Db::name($this->table)->insertGetId($p);
            }
            else{
                $data=$p;
                unset($data['id']);
                $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update($data);
            }
            return jsonReturn(1,'保存成功',$p);
        }
        $data=array();
        // $city=Db::name('city')->select()->toarray();
        // View::assign('city',$city);
        $danwei_county='';
        if(isset($p['id']))
        {
            $data = Db::name($this->table)->where(['id'=>intval($p['id'])])->find();
            // $county=Db::name('county')->where('city_id',$data['city_id'])->select()->toarray();
            
            if(!empty($data['county_id']))
            {
                $danwei_county=$data['county_id'];
            }
            //$danwei_county=Db::name('county')->alias('c')->join('DanweiCounty dc','dc.county_id=c.id')->field('c.*')->where(['dc.danwei_id'=>$data['id']])->field('c.id,c.name')->select()->toarray();
            //$danwei_county = "'".str_replace(",","','",$user['county'])."'";
        }
        // else
        // {
        //     $county=Db::name('county')->where('city_id',$city[0]['id'])->select()->toarray();
        // }
        View::assign('data',$data);
        View::assign('danwei_county',$danwei_county);
        // View::assign('county',$county);
        return View::fetch();
    }
    
    public function status()
    {
        $p = input();
        $res = Db::name($this->table)->where(['id'=>intval($p['id'])])->update(['status'=>intval($p['status'])]);
        return jsonRes($res);
    }

    public function remove()
    {
        $p=input();
        $id=$p['id'];
        if(!is_array($id))
        {
            $id=explode(',',$p['id']);
        }
        $res = Db::name($this->table)->where('id','in',$id)->delete();
        if($res>0)
        {
            return jsonReturn(1);
        }
        return jsonReturn(0);
    }    
}